package bma.common.langutil.jmshell.common;

import java.util.ArrayList;
import java.util.List;

import bma.common.langutil.jmshell.JMShellProcessor;
import bma.common.langutil.jmshell.JMShellUtil;
import bma.common.langutil.jmshell.Session;
import bma.common.langutil.jmshell.command.CommandProcessorWrap;
import bma.common.langutil.jmshell.command.CommandSet;

public class JMShellProcessor4Link implements JMShellProcessor, CommandSet {

	protected List<JMShellProcessor> processors;

	public List<JMShellProcessor> getProcessors() {
		return processors;
	}

	public void setProcessors(List<JMShellProcessor> processors) {
		this.processors = processors;
	}

	public void addProcessor(JMShellProcessor p) {
		if (this.processors == null) {
			this.processors = new ArrayList<JMShellProcessor>();
		}
		this.processors.add(p);
	}

	public void addCommand(String cmd, JMShellProcessor p, String help) {
		CommandProcessorWrap o = new CommandProcessorWrap(cmd);
		o.setProcessor(p);
		o.setHelp(help);
		addProcessor(o);
	}

	@Override
	public boolean process(Session session, String text) {
		if (processors != null) {
			for (JMShellProcessor p : processors) {
				if (p.process(session, text))
					return true;
			}
		}
		return false;
	}

	@Override
	public void listCommands(Session session, List<String> results) {
		if (processors != null) {
			for (JMShellProcessor p : processors) {
				JMShellUtil.handleListCommand(session, results, p);
			}
		}
	}
}
